package com.mdd.maduoduo.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mdd.maduoduo.R;
import com.mdd.maduoduo.adapter.ShareCodeAdapter;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.Response;
import com.mdd.maduoduo.entity.ShareCodeEntity;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.AlertUtils;
import com.mdd.maduoduo.utils.BottomSheetUtils;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.utils.Utils;
import com.mdd.maduoduo.view.interfaces.AddShareCodeView;
import com.mdd.maduoduo.view.interfaces.DeleteShareCodeView;
import com.mdd.maduoduo.view.interfaces.GetShareView;

import javax.inject.Inject;

public class ShareCodeActivity extends BaseActivity implements AddShareCodeView, GetShareView, ShareCodeAdapter.ClickDeleteListener, DeleteShareCodeView {

    @Inject
    public LoginPresenter mPresenter;

    private ImageView ivBack;

    private TextView tvSelect;

    private BottomSheetUtils bottomSheetUtils;

    private String mRole;

    private EditText etCode;

    private TextView tvAdd;

    private ShareCodeAdapter mAdapter;

    private RecyclerView recyclerView;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_share_code;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setAddShareCodeView(this);
        mPresenter.setGetShareView(this);
        mPresenter.setDeleteShareCodeView(this);
    }

    @Override
    protected void initView() {
        super.initView();
        bottomSheetUtils = new BottomSheetUtils();
        ivBack = findViewById(R.id.ivBack);
        tvSelect = findViewById(R.id.tvSelect);
        etCode = findViewById(R.id.etCode);
        tvAdd = findViewById(R.id.tvAdd);
        recyclerView = findViewById(R.id.recyclerView);

        mPresenter.getShareCode(ShareCodeActivity.this);

        mAdapter = new ShareCodeAdapter();
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setNestedScrollingEnabled(false);
        mAdapter.setListener(this);
    }

    @Override
    protected void onClick() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheetUtils.selectRoleAlert(ShareCodeActivity.this, new BottomSheetUtils.SelectRoleListener() {
                    @Override
                    public void sureSelect(String name) {
                        if (name.equals("其他")){
                            AlertUtils.normaEtlAlert(ShareCodeActivity.this, "请输入角色", new AlertUtils.SelectRoleListener() {
                                @Override
                                public void sureSelect(String role) {
                                    mRole = role;
                                    tvSelect.setText(mRole);
                                    AlertUtils.dismiss();
                                }
                            });
                        }else {
                            mRole = name;
                            tvSelect.setText(mRole);
                        }
                        bottomSheetUtils.dismiss();

                    }
                });
            }
        });

        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                    //添加共享码
                    String pickupSharedCode = etCode.getText().toString();
                    if (pickupSharedCode!=null&&!pickupSharedCode.isEmpty()){
                        if (mRole!=null&&!mRole.isEmpty()){
                            mPresenter.addShareCode(ShareCodeActivity.this,pickupSharedCode,mRole);
                        }else{
                            ToastUtils.showLongToast(ShareCodeActivity.this,"请选择角色");
                        }
                    }else {
                        ToastUtils.showLongToast(ShareCodeActivity.this,"请输入对方的共享码");
                    }
                }
            }
        });
    }

    @Override
    public void renderSuccess(Response response) {
        ToastUtils.showLongToast(ShareCodeActivity.this,"添加成功");
        etCode.setText("");
        mRole = "";
        tvSelect.setText("选择角色");
        mPresenter.getShareCode(ShareCodeActivity.this);
    }

    @Override
    public void renderSuccess(ShareCodeEntity shareCodeEntity) {
        mAdapter.setData(shareCodeEntity.data.records);
    }

    @Override
    public void delete(ShareCodeEntity.DataDTO.RecordsDTO data) {
        AlertUtils.normalAlert(ShareCodeActivity.this, "温馨提示", "确定要删除共享码吗?", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.deleteShareCode(ShareCodeActivity.this,data.pickupSharedCode);
                AlertUtils.dismiss();
            }
        });
    }

    @Override
    public void renderSuccess(Response response, int position) {
        ToastUtils.showLongToast(ShareCodeActivity.this,"删除成功");
        mPresenter.getShareCode(ShareCodeActivity.this);
    }
}
